package com.lfs.feishu.controller;

import com.lark.oapi.Client;
import com.lark.oapi.core.response.EmptyData;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.google.gson.JsonParser;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.service.im.v1.model.*;

import java.nio.charset.StandardCharsets;

import static com.lfs.feishu.constant.AppInfo.APP_ID;
import static com.lfs.feishu.constant.AppInfo.APP_SECRET;

@AllArgsConstructor
@RestController
@RequestMapping("/feishu/single")
@Slf4j
public class FeishuSingleWithdrawController {


    /**
     * 单条消息撤回
     * @param msgId
     * @throws Exception
     */
    @GetMapping("/withdraw")
    public void withdraw(@RequestParam String msgId) throws Exception {
        // 构建client
        Client client = Client.newBuilder(APP_ID, APP_SECRET).build();

        // 创建请求对象
        DeleteMessageReq req = DeleteMessageReq.newBuilder()
                .messageId(msgId)
                .build();

        // 发起请求
        DeleteMessageResp resp = client.im().v1().message().delete(req);

        // 处理服务端错误
        if (!resp.success()) {
            log.error("删除消息失败: {}", Jsons.createGSON(true, false).toJson(JsonParser.parseString(new String(resp.getRawResponse().getBody(), StandardCharsets.UTF_8))));
            throw new RuntimeException("删除消息失败");
        }
    }

}
